<?php

namespace app\health\behavior;

use app\health\model\HealthCart;
use app\health\service\ConfigService;
use app\health\service\SystemNoticeService;
use http\Env;

class OrderListener
{
    /**
     * 订单创建后
     * @param $params
     * @return void
     * @throws \RedisException
     */
    public function orderCreateAfter(&$params)
    {
        $orderConfig = ConfigService::getOrderConfig();
        // 如有购物车订单ID则删除购物车数据
        if (!empty($params['cart_id'])) {
            HealthCart::whereIn('id', $params['cart_id'])->delete();
        }
        if (APP_ENV !== 'dev') {
            queue_custom('app\common\job\GoodsOrderAutoCancelJob', [
                'id' => $params['order_id'],
            ], $orderConfig['order_auto_close'] ?? 86400);
        }
    }

    /**
     * 订单支付成功
     * @param $params
     * @return void
     * @throws \RedisException
     */
    public function orderPayedAfter(&$params): void
    {
        // 添加更新商品销售数量队列任务
        queue_custom('app\common\job\GoodsOrderAutoSalesJob', [
            'order_id' => $params['order_id'],
        ]);
        //触发新订单通知
        queue_custom('app\common\job\SystemNotifyMessageJob', [
            'method' => 'newGoodsOrderMessage',
            'data'   => [
                'order_id' => $params['order_id'],
            ]
        ]);
        //完成首次购买商品任务
        queue_custom('app\common\job\WebsiteTaskJob', [
            'user_id'      => $params['user_id'],
            'complete_way' => 5
        ], 10);
    }
}
